<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable class="!w-240px">
          <el-option label="待付款" value="0" />
          <el-option label="待审核" value="10" />
          <el-option label="进行中" value="20" />
          <el-option label="已结束" value="30" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          重置
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="用户信息" align="center" min-width="100px">
        <template #default="scope">
          <el-avatar :src="scope.row.user?.avatar" /><br />
          <el-tooltip :content="'用户编号：' + scope.row.userId" placement="top">
            <el-text>{{ scope.row.advertiser || scope.row.user?.nickname }}</el-text>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="价格信息" align="left" min-width="120px">
        <template #default="scope">
          <el-text>总预算：{{ fenToYuan(scope.row.budgetPrice) }}元</el-text>
          <br />
          <el-text
            >已花费：<span class="text-red-500">{{ fenToYuan(scope.row.spendPrice) }}</span
            >元</el-text
          >
        </template>
      </el-table-column>
      <el-table-column label="投放效果" align="center" min-width="180px">
        <template #default="scope">
          <p
            >投放{{ scope.row.exceptDuration }}秒，即{{
              (scope.row.exceptDuration / 60 / 60).toFixed(0)
            }}小时</p
          >
          <p
            >预期曝光 {{ scope.row.exceptExposureCount }} 次、播放
            {{ scope.row.exceptPlayCount }} 次</p
          >
          <p
            >实际曝光 {{ scope.row.actualExposureCount }} 次、播放
            {{ scope.row.actualPlayCount }} 次</p
          >
        </template>
      </el-table-column>
      <el-table-column label="视频" align="center" min-width="150px">
        <template #default="scope">
          <VideoPlayer
            :title="scope.row.title"
            :url="scope.row.videoUrl"
            :poster="scope.row.imageUrl"
          />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" min-width="100px">
        <template #default="scope">
          <div>
            <el-tag v-if="scope.row.status === 0" type="warning">待付款</el-tag>
            <el-tag v-else-if="scope.row.status === 10" type="warning">待审核</el-tag>
            <el-tag v-else-if="scope.row.status === 20" type="success">进行中</el-tag>
            <el-tag v-else-if="scope.row.status === 30" type="info">已结束</el-tag>
            <el-tag v-else type="primary">未知</el-tag>
            <br v-if="scope.row.status === 30" />
            <el-text v-if="scope.row.status === 30">原因：{{ scope.row.note }}</el-text>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="时间" align="center" width="180px">
        <template #default="scope">
          <el-text>{{ formatDate(scope.row.createTime) || '--' }}</el-text>
        </template>
      </el-table-column>
      <el-table-column
        v-hasRole="['super_admin', 'platform_admin', 'tenant_admin']"
        label="操作"
        align="center"
        fixed="right"
        width="120px"
      >
        <template #default="scope">
          <el-button
            link
            type="warning"
            @click="openAuditForm(scope.row.id)"
            :disabled="scope.row.status !== 10"
          >
            审核
          </el-button>
          <br />
          <el-button
            link
            type="primary"
            @click="openDetailForm(scope.row.id)"
            :disabled="scope.row.status !== 20 && scope.row.status !== 30"
          >
            查看明细
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <AuditForm ref="auditFormRef" @success="getList" />
  <DetailForm ref="detailFormRef" />
</template>

<script setup lang="ts">
import { AdApi } from '@/api/ad/plan'
import AuditForm from './AuditForm.vue'
import DetailForm from './DetailForm.vue'
import { fenToYuan } from '@/utils'
import { formatDate } from '@/utils/formatTime'

/** 广告投放计划 列表 */
defineOptions({ name: 'AdPlan' })

const loading = ref(true) // 列表的加载中
const list = ref<any>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  status: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await AdApi.getPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const auditFormRef = ref()

const openAuditForm = (id: number | string) => {
  auditFormRef.value.open('update', id)
}

/** 查看明细 */
const detailFormRef = ref()

const openDetailForm = (id: number | string) => {
  detailFormRef.value.open(id)
}

/** 初始化 **/
onMounted(async () => {
  await getList()
})
</script>
